package com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationCustomColumn;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationCustomColumnValue;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanChargeCategorySetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.RxRegistrationPlanSetting;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.resp.RxRegistrationChargeCategoryDetailResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.resp.RxRegistrationChargeCategoryResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationCustomColumnMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationCustomColumnValueMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.mapper.RxRegistrationPlanChargeCategorySettingMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.RxRegistrationAdvanceStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.resp.RxRegistrationAdvanceCustomColumnValueResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.resp.RxRegistrationAdvanceStudentInfoResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.mapper.RxRegistrationAdvanceStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.util.PlanUtil;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.RxRegistrationWelcomeReduction;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.entity.resp.RxRegistrationPlanChargeCategoryResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.mapper.RxRegistrationWelcomeReductionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistration.constant.AppTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistration.constant.ChargeCategoryTypeConstant.CHARGE_CATEGORY_TYPE_MAJOR;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistration.constant.ChargeCategoryTypeConstant.CHARGE_CATEGORY_TYPE_NORMAL;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.constant.RegistrationStatusConstant.REGISTRATION_STATUS_FINISH;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationWelcome.constant.AllowStatusConstant.ALLOW_STATUS_PASS;


@Component
public class WelcomeStuUtil {

    @Resource
    private PlanUtil planUtil;
    @Resource
    private RxRegistrationAdvanceStudentInfoMapper rxRegistrationAdvanceStudentInfoMapper;
    @Resource
    private RxRegistrationPlanChargeCategorySettingMapper rxRegistrationPlanChargeCategorySettingMapper;
    @Resource
    private RxRegistrationCustomColumnMapper rxRegistrationCustomColumnMapper;
    @Resource
    private RxRegistrationCustomColumnValueMapper rxRegistrationCustomColumnValueMapper;
    @Resource
    private RxRegistrationWelcomeReductionMapper rxRegistrationWelcomeReductionMapper;

    /**
     *
     * @param studentInfo 学生信息
     * @return 学生要缴费项
     */
    public RxRegistrationAdvanceStudentInfoResp getWelcomeAllStuPaymentCategory(RxRegistrationAdvanceStudentInfo studentInfo) {
        RxRegistrationAdvanceStudentInfoResp resp = new RxRegistrationAdvanceStudentInfoResp();
        List<RxRegistrationPlanChargeCategoryResp> respList;
        if (studentInfo.getAdvanceStatus() == REGISTRATION_STATUS_FINISH
                && studentInfo.getConfirmStatus() == REGISTRATION_STATUS_FINISH
                && studentInfo.getWelcomeStatus() == REGISTRATION_STATUS_FINISH) {
            resp.setIsSetMajor(true);
            return resp;
        }
        //先判断学生是否申请减免 缓交 只有通过才判断
        Integer reductionStatus = studentInfo.getReductionStatus();//减免状态
        Integer delayStatus = studentInfo.getDelayStatus(); //缓交状态
        //缓交通过时
        if (delayStatus == ALLOW_STATUS_PASS) {
            studentInfo.setWelcomeStatus(REGISTRATION_STATUS_FINISH);
            studentInfo.setAdvanceStatus(REGISTRATION_STATUS_FINISH);
            studentInfo.setConfirmStatus(REGISTRATION_STATUS_FINISH);
            rxRegistrationAdvanceStudentInfoMapper.updateById(studentInfo);
            resp.setIsSetMajor(true);
            return resp;
        }

        //减免通过时
        if (reductionStatus == ALLOW_STATUS_PASS) {
            List<RxRegistrationWelcomeReduction> welcomeReductionList = rxRegistrationWelcomeReductionMapper.selectList(new LambdaQueryWrapper<RxRegistrationWelcomeReduction>()
                    .eq(RxRegistrationWelcomeReduction::getStuId, studentInfo.getId())
                    .eq(RxRegistrationWelcomeReduction::getAllowStatus, ALLOW_STATUS_PASS));
            if (welcomeReductionList != null && welcomeReductionList.size() > 0) {
                RxRegistrationWelcomeReduction rxRegistrationWelcomeReduction = welcomeReductionList.get(0);
                String reductionCategory = rxRegistrationWelcomeReduction.getReductionCategory();
                if (!isNullOrEmpty(reductionCategory)) {
                    respList = JSON.parseArray(reductionCategory, RxRegistrationPlanChargeCategoryResp.class);
                    for (RxRegistrationPlanChargeCategoryResp rxRegistrationPlanChargeCategoryResp : respList) {
                        if(rxRegistrationPlanChargeCategoryResp.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR ){
                            List<RxRegistrationChargeCategoryDetailResp> collect = rxRegistrationPlanChargeCategoryResp.getChildList().stream()
                                    .filter(i -> i.getChargeCategoryName().equals(studentInfo.getGroupMajor())).collect(Collectors.toList());
                            if (collect.size() > 0) {
                                resp.setIsSetMajor(true);//有专业收费项
                            } else {
                                resp.setIsSetMajor(false);//没有专业收费项
                            }
                        }
                    }

                    resp.setChargeCategoryRespList(respList);
                    return resp;
                }
            }

        }
        LambdaQueryWrapper<RxRegistrationPlanChargeCategorySetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RxRegistrationPlanChargeCategorySetting::getPlanId, studentInfo.getPlanId())
                .and(wrapper -> {
                    if (studentInfo.getAdvanceStatus() != REGISTRATION_STATUS_FINISH) {
                        wrapper.eq(RxRegistrationPlanChargeCategorySetting::getAppType, APP_TYPE_ADVANCE);
                    }
                    if (studentInfo.getConfirmStatus() != REGISTRATION_STATUS_FINISH) {
                        wrapper.or()
                                .eq(RxRegistrationPlanChargeCategorySetting::getAppType, APP_TYPE_CONFIRM);
                    }
                    if (studentInfo.getWelcomeStatus() != REGISTRATION_STATUS_FINISH) {
                        wrapper.or()
                                .eq(RxRegistrationPlanChargeCategorySetting::getAppType, APP_TYPE_WELCOME);
                    }
                })
                .orderByAsc(RxRegistrationPlanChargeCategorySetting::getAppType);
        List<RxRegistrationPlanChargeCategorySetting> categorySettingList = rxRegistrationPlanChargeCategorySettingMapper.selectList(queryWrapper);
        respList = categorySettingList
                .stream()
                .map(setting -> {
                    RxRegistrationPlanChargeCategoryResp chargeCategoryResp = new RxRegistrationPlanChargeCategoryResp();
                    BeanUtils.copyProperties(setting, chargeCategoryResp);
                    //子项收费项(封装不是一般收费项的和专业收费项)
                    List<RxRegistrationChargeCategoryDetailResp> childList = new ArrayList<>();
                    if (setting.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_NORMAL && setting.getChargeCategoryType() != CHARGE_CATEGORY_TYPE_MAJOR) {
                        childList = JSONObject.parseArray(setting.getChargeCategoryChildren(), RxRegistrationChargeCategoryDetailResp.class);
                    }
                    //收费项单独处理
                    if (setting.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR) {
                        String chargeCategoryChildren = setting.getChargeCategoryChildren();
                        if (!isNullOrEmpty(chargeCategoryChildren)) {
                            List<RxRegistrationChargeCategoryDetailResp> chargeCategoryList = JSON.parseArray(chargeCategoryChildren, RxRegistrationChargeCategoryDetailResp.class);
                            if (chargeCategoryList != null && chargeCategoryList.size() > 0) {
                                //匹配学生信息中专业的收费项
                                List<RxRegistrationChargeCategoryDetailResp> collect = chargeCategoryList.stream()
                                        .filter(i -> i.getChargeCategoryName().equals(studentInfo.getGroupMajor())).collect(Collectors.toList());
                                if (collect.size() > 0) {
                                    resp.setIsSetMajor(true);//有专业收费项
                                } else {
                                    resp.setIsSetMajor(false);//没有专业收费项
                                }
                                for (RxRegistrationChargeCategoryDetailResp rxRegistrationChargeCategoryResp : chargeCategoryList) {
                                    if (rxRegistrationChargeCategoryResp.getChargeCategoryName().equals(studentInfo.getGroupMajor())) {
                                        RxRegistrationChargeCategoryDetailResp chargeCategoryDetailResp = new RxRegistrationChargeCategoryDetailResp();
                                        BeanUtils.copyProperties(rxRegistrationChargeCategoryResp, chargeCategoryDetailResp);
                                        childList.add(chargeCategoryDetailResp);
                                    }
                                }
                            }

                        }
                    }
                    chargeCategoryResp.setChildList(childList);
                    return chargeCategoryResp;
                }).collect(Collectors.toList());

        resp.setChargeCategoryRespList(respList);
        return resp;

    }

//    /**
//     * 获取学生第三阶段应该缴的收费项
//     * @param idCard 身份证id
//     * @return 获取学生第三阶段应该缴的收费项
//     */
//    public List<RxRegistrationPlanChargeCategoryResp> getWelcomeStuShouldPaymentCategory(String idCard){
//
//        //获取启用计划id
//        RxRegistrationPlanSetting enablePlanInfo = planUtil.getEnablePlanInfo();
//        if (enablePlanInfo == null) {
//            return null;
//        }
//        String planId = enablePlanInfo.getId();
//        List<RxRegistrationAdvanceStudentInfo> studentInfoList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
//                .eq(RxRegistrationAdvanceStudentInfo::getPlanId, planId)
//                .eq(RxRegistrationAdvanceStudentInfo::getIdCard, idCard));
//        if (studentInfoList == null || studentInfoList.size() == 0) {
//            return null;
//        }
//        RxRegistrationAdvanceStudentInfo studentInfo = studentInfoList.get(0);
//        //缴费状态  0:未完成 1:进行中 2: 完成
//        Integer advanceStatus = studentInfo.getAdvanceStatus();//预报名阶段状态
//        Integer confirmStatus = studentInfo.getConfirmStatus();  //确认报名
//        Integer welcomeStatus = studentInfo.getWelcomeStatus();//迎新报名
//        //先判断学生是否申请减免 缓交 只有通过才判断
//        Integer reductionStatus = studentInfo.getReductionStatus();//减免状态
//        Integer delayStatus = studentInfo.getDelayStatus(); //缓交状态
//        //缓交通过时
//        if (delayStatus == ALLOW_STATUS_PASS) {
//            return null;
//        }
//        // 获取收费项
//        List<RxRegistrationPlanChargeCategoryResp> chargeCategoryList = new ArrayList<>();
//        //减免通过时,缴费减免后的缴费项
//        if (reductionStatus == ALLOW_STATUS_PASS) {
//            List<RxRegistrationWelcomeReduction> welcomeReductionList = rxRegistrationWelcomeReductionMapper.selectList(new LambdaQueryWrapper<RxRegistrationWelcomeReduction>()
//                    .eq(RxRegistrationWelcomeReduction::getStuId, studentInfo.getId())
//                    .eq(RxRegistrationWelcomeReduction::getAllowStatus, ALLOW_STATUS_PASS));
//            if (welcomeReductionList != null && welcomeReductionList.size() > 0) {
//                RxRegistrationWelcomeReduction rxRegistrationWelcomeReduction = welcomeReductionList.get(0);
//                String reductionCategory = rxRegistrationWelcomeReduction.getReductionCategory();
//                if (!isNullOrEmpty(reductionCategory)) {
//                    chargeCategoryList = JSON.parseArray(reductionCategory, RxRegistrationPlanChargeCategoryResp.class);
//
//                }
//            }
//
//        }else {//未通过则按后台设置的缴费项判断
//
//            List<Integer> appTypeList = new ArrayList<>();
//            if (advanceStatus != REGISTRATION_STATUS_FINISH) {//第一阶段未缴费
//                appTypeList.add(APP_TYPE_ADVANCE);
//            }
//            if (confirmStatus != REGISTRATION_STATUS_FINISH) {//第二阶段未交费
//                appTypeList.add(APP_TYPE_CONFIRM);
//            }
//            if (welcomeStatus != REGISTRATION_STATUS_FINISH){
//                appTypeList.add(APP_TYPE_WELCOME);
//            }
//            List<RxRegistrationPlanChargeCategorySetting> categorySettingList = rxRegistrationPlanChargeCategorySettingMapper.selectList(new LambdaQueryWrapper<RxRegistrationPlanChargeCategorySetting>()
//                    .eq(RxRegistrationPlanChargeCategorySetting::getPlanId, planId)
//                    .orderByAsc(RxRegistrationPlanChargeCategorySetting::getAppType)
//                    .in(RxRegistrationPlanChargeCategorySetting::getAppType, appTypeList));
//            if (categorySettingList == null || categorySettingList.size() == 0) {
//                return null;
//            }
//            chargeCategoryList = categorySettingList
//                    .stream()
//                    .map(rxRegistrationPlanChargeCategorySetting -> {
//                        RxRegistrationPlanChargeCategoryResp categorySettingResp = new RxRegistrationPlanChargeCategoryResp();
//                        //专业收费项
//                        if (rxRegistrationPlanChargeCategorySetting.getChargeCategoryType() == CHARGE_CATEGORY_TYPE_MAJOR) {
//                            String chargeCategoryChildren = rxRegistrationPlanChargeCategorySetting.getChargeCategoryChildren();
//                            if (!isNullOrEmpty(chargeCategoryChildren)) {
//                                List<RxRegistrationChargeCategoryResp> chargeCategoryRespList = JSON.parseArray(chargeCategoryChildren, RxRegistrationChargeCategoryResp.class);
//                                if (chargeCategoryRespList != null && chargeCategoryRespList.size() > 0) {
//                                    List<RxRegistrationChargeCategoryResp> collect = chargeCategoryRespList.stream()
//                                            .filter(i -> i.getChargeCategoryName().equals(studentInfo.getGroupMajor())).collect(Collectors.toList());
//                                    if (collect.size() < 1) {
//                                        ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_NOT_SET_MAJOR));//没有专业收费项
//                                    }
//                                    //匹配学生信息中专业的收费项
//                                    for (RxRegistrationChargeCategoryResp rxRegistrationChargeCategoryResp : chargeCategoryRespList) {
//                                        if (rxRegistrationChargeCategoryResp.getChargeCategoryName().equals(studentInfo.getGroupMajor())) {
//                                            BeanUtils.copyProperties(rxRegistrationPlanChargeCategorySetting, categorySettingResp);
//                                            categorySettingResp.setChargeCategoryPrice(rxRegistrationChargeCategoryResp.getChargeCategoryPrice());//所选专业的收费价格
//
//                                        }
//
//
//                                    }
//
//                                }
//
//                            }
//                        } else {
//                            BeanUtils.copyProperties(rxRegistrationPlanChargeCategorySetting, categorySettingResp);
//
//                        }
//                        return categorySettingResp;
//                    }).collect(Collectors.toList());
//        }
//        return chargeCategoryList;
//    }

    /**
     * 迎新报名 自定义字段
     * @param idCard 身份证
     * @return 迎新报名 自定义字段
     */
    public List<RxRegistrationAdvanceCustomColumnValueResp> getWelcomeStuCustomColumnValueList(String idCard){
        List<RxRegistrationAdvanceCustomColumnValueResp> respList = new ArrayList<>();
        //获取启用计划id
        RxRegistrationPlanSetting enablePlanInfo = planUtil.getEnablePlanInfo();
        if (enablePlanInfo == null) {
            return null;
        }
        String planId = enablePlanInfo.getId();
        List<RxRegistrationAdvanceStudentInfo> studentInfoList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                .eq(RxRegistrationAdvanceStudentInfo::getPlanId, planId)
                .eq(RxRegistrationAdvanceStudentInfo::getIdCard, idCard));
        //学生信息不存在,则没有经过第一 第二阶段 直接到第三阶段
        List<RxRegistrationCustomColumn> customColumnList = rxRegistrationCustomColumnMapper.selectList(new LambdaQueryWrapper<RxRegistrationCustomColumn>()
                .eq(RxRegistrationCustomColumn::getPlanId, planId)
                .orderByAsc(RxRegistrationCustomColumn::getAppType)
                .orderByAsc(RxRegistrationCustomColumn::getColumnSort));
        if (studentInfoList == null || studentInfoList.size() == 0) {
            respList = customColumnList
                    .stream()
                    .map(rxRegistrationCustomColumn -> {
                        RxRegistrationAdvanceCustomColumnValueResp resp = new RxRegistrationAdvanceCustomColumnValueResp();
                        BeanUtils.copyProperties(rxRegistrationCustomColumn, resp,"id");
                        resp.setColumnId(rxRegistrationCustomColumn.getId());
                        return resp;
                    })
                    .collect(Collectors.toList());
        }else {//有学生信息,则判断自定义字段
            RxRegistrationAdvanceStudentInfo studentInfo = studentInfoList.get(0);
            String stuId = studentInfo.getId();
            if (customColumnList != null && customColumnList.size() > 0) {
                for (RxRegistrationCustomColumn rxRegistrationCustomColumn : customColumnList) {
                    RxRegistrationAdvanceCustomColumnValueResp resp = new RxRegistrationAdvanceCustomColumnValueResp();
                    //设置的自定义字段和添加的数据对比
                    List<RxRegistrationCustomColumnValue> columnValueList = rxRegistrationCustomColumnValueMapper.selectList(new LambdaQueryWrapper<RxRegistrationCustomColumnValue>()
                            .eq(RxRegistrationCustomColumnValue::getPlanId, planId)
                            .orderByAsc(RxRegistrationCustomColumnValue::getColumnSort)
                            .eq(RxRegistrationCustomColumnValue::getStudentId, stuId)
                            .eq(RxRegistrationCustomColumnValue::getColumnId, rxRegistrationCustomColumn.getId()));
                    //有数据 则返回数据和字段名
                    if (columnValueList != null && columnValueList.size() > 0) {
                        BeanUtils.copyProperties(columnValueList.get(0), resp);
                        resp.setColumnId(rxRegistrationCustomColumn.getId());
                    } else {//没有填过数据 则返回字段名
                        BeanUtils.copyProperties(rxRegistrationCustomColumn, resp);
                        resp.setColumnId(rxRegistrationCustomColumn.getId());
                    }
                    respList.add(resp);
                }
            }
        }
        return respList;
    }
}
